<template>
    <d2-container>
        <template slot="header">
            <el-form :inline="true" :model="searchForm" ref="searchForm" size="small" v-if="showHeader" class="searchHeader">

                <el-form-item label="参数范围" prop="dictMode">
                    <ec-dict-select v-model="searchForm.dictMode" placeholder="请选择参数范围" dictType="dict_mode">
                    </ec-dict-select>
                </el-form-item>

                <el-form-item label="参数分类" prop="dictType">
                    <el-input v-model="searchForm.dictType" placeholder="请输入参数分类" clearable />
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="getDataTable">
                        <d2-icon name="search" />
                        查询
                    </el-button>
                    <el-button @click="handleFormReset">
                        <d2-icon name="refresh" />
                        重置
                    </el-button>
                </el-form-item>

            </el-form>
        </template>

        <div class="table-header">
            <div>
                <el-button type="primary" icon="el-icon-circle-plus-outline" size="small" @click="addOrUpdate()">添加字典
                </el-button>
                <el-button type="primary" icon="el-icon-document-copy" size="small" @click="copyHandler()">复制
                </el-button>
                <el-button type="primary" icon="el-icon-delete" size="small" @click="delHandler()">批量删除</el-button>
            </div>
            <div>
                <ec-prompt-button content="刷新" icon="el-icon-refresh" @click="getDataTable()"></ec-prompt-button>
                <ec-prompt-button content="搜索" icon="el-icon-search" @click="changeHeader()"></ec-prompt-button>
                <ec-prompt-button content="导入" icon="el-icon-download"></ec-prompt-button>
                <ec-prompt-button content="导出" icon="el-icon-upload2"></ec-prompt-button>
                <ec-prompt-button content="分享" icon="el-icon-share"></ec-prompt-button>
            </div>
        </div>

        <ec-table :data="tabledata" v-loading="loading" @selection-change="handleSelectionChange">
            <el-table-column type="selection" header-align="center" align="center" width="50">
            </el-table-column>
            <el-table-column prop="dictType" header-align="center" align="center" label="参数分类">
                <template slot-scope="scope">
                    <el-link type="primary" @click="showDictDetails(scope.row.pkType)">{{scope.row.dictType}}
                    </el-link>
                </template>
            </el-table-column>
            <el-table-column prop="dictDesc" header-align="center" align="center" label="参数描述">
            </el-table-column>
            <el-table-column prop="dictModeDesc" header-align="center" align="center" label="字典范围">
            </el-table-column>
            <el-table-column prop="dictNote" header-align="center" align="center" label="字典备注">
            </el-table-column>
            <el-table-column prop="createdByName" header-align="center" align="center" label="创建人">
            </el-table-column>
            <el-table-column prop="createdTime" header-align="center" align="center" label="创建时间">
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="200" header-align="center" align="center">
                <template slot-scope="scope">
                    <el-button type="text" size="small" @click="addOrUpdate(scope.row.pkType)"><i
                            class="el-icon-edit"></i>编辑</el-button>
                    <el-button type="text" size="small" @click="deleteDict(scope.row.pkType)"><i
                            class="el-icon-delete"></i>删除
                    </el-button>
                    <el-button type="text" size="small" @click="showDictDetails(scope.row.pkType)"><i
                            class="el-icon-menu"></i>字典项</el-button>
                </template>
            </el-table-column>
        </ec-table>
        <template slot="footer">
            <!-- <el-button type="primary" size="mini" :loading="uploading" style="float:left;">
                <d2-icon name="cloud-upload" />
                Upload log data
            </el-button> -->

            <ec-pagination :page="page" @size-change="handleSizeChange" @current-change="handleCurrentChange">
            </ec-pagination>
        </template>

        <add-dict-type v-if="dictTypeVisible" ref="dictType" @refreshDataList="getDataTable"></add-dict-type>
        <dict-detail v-if="dictDetailsVisible" ref="dictDetail" @refreshDataList="getDataTable"></dict-detail>
    </d2-container>
</template>

<script>
    import * as dictService from '@/api/bdms/dict'
    import addDictType from './addDictType'
    import dictDetail from './dictDetail'
    export default {
        name: "system-dict-index",
        data() {
            return {
                searchForm: {
                    dictMode: '',
                    dictType: '',
                },
                page: {
                    page: 1,
                    limit: 10,
                    total: 0
                },
                showHeader: true,
                dictDetailsVisible: false,
                dictTypeVisible: false,
                tabledata: [],
                multipleSelection: [],
                loading: false,
            }
        },
        components: {
            dictDetail,
            addDictType
        },
        computed: {},
        mounted() {
            this.getDataTable();
        },
        methods: {
            getDataTable() {
                this.loading = true;
                let query = {
                    page: this.page.page,
                    limit: this.page.limit,
                    dictMode: this.searchForm.dictMode,
                    dictType: this.searchForm.dictType
                };
                dictService.getDictTypeList(query).then(data => {
                    this.tabledata = data.list;
                    this.page.total = data.totalCount;
                    this.loading = false;
                }).catch((res) => {
                    this.loading = false;
                });
            },
            showDictDetails(pkType) {
                this.dictDetailsVisible = true;
                this.$nextTick(() => {
                    this.$refs.dictDetail.init(pkType)
                })
            },
            addOrUpdate(id) {
                this.dictTypeVisible = true;
                this.$nextTick(() => {
                    this.$refs.dictType.init(id)
                })
            },
            copyHandler() {
                var ids = this.multipleSelection.map(item => {
                    return item.pkType
                })
                if (ids.length > 0) {
                    this.$confirm("确认复制？", "确认信息", {
                        distinguishCancelAndClose: true,
                        confirmButtonText: "确定",
                        cancelButtonText: "取消"
                    }).then(() => {
                        dictService.dictcopy(ids).then(() => {
                            this.getDataTable();
                        });
                    });
                } else {
                    this.$message.warning("请至少选择一项");
                }
            },
            // 批量删除
            delHandler() {
                var ids = this.multipleSelection.map(item => {
                    return item.pkType
                })
                if (ids.length > 0) {
                    this.$confirm("确认删除？", "确认信息", {
                        distinguishCancelAndClose: true,
                        confirmButtonText: "删除",
                        cancelButtonText: "取消"
                    }).then(() => {
                        dictService.delBatchDictType(ids).then(() => {
                            this.getDataTable();
                        });
                    });
                } else {
                    this.$message.warning("请至少选择一项");
                }
            },
            deleteDict(id) {
                if (id) {
                    this.$confirm("确认删除？", "确认信息", {
                        distinguishCancelAndClose: true,
                        confirmButtonText: "删除",
                        cancelButtonText: "取消"
                    }).then(() => {
                        dictService.delDictType(id).then(() => {
                            this.getDataTable();
                        });
                    });
                }

            },
            changeHeader() {
                this.showHeader = !this.showHeader
            },
            handleFormReset() {
                this.$refs.searchForm.resetFields()
            },
            //分页查询
            handleSizeChange(val) {
                this.page.limit = val;
                this.getDataTable();
            },
            handleCurrentChange(val) {
                this.page.page = val;
                this.getDataTable();
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
        }
    }
</script>